package spring.cloud.tasks.api.vo;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import spring.cloud.tasks.api.domain.executor.Executor;
import spring.cloud.tasks.api.domain.task.TaskConfiguration;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Setter
@Getter
public class TaskConfigurationVo {

    private String taskId;
    private String jobClass;
    private Integer shardingTotalCount;

    private String cron;
    private List<String> pausePeriodDate;
    private List<String> pausePeriodTime;
    private String shardingItemParameters;
    private String jobParameter;
    private Integer processCountIntervalSeconds;

    private String description;
    private Integer timeout4AlarmSeconds;
    private Integer timeoutSeconds;
    private Integer disableTimeoutSeconds;
    private Boolean showNormalLog;

    private String jobType;

    private Integer loadLevel;


    private String enabled;
    private List<String> preferExecutorIdList;


    private Boolean rerun;

    private List<Executor> executorList;


    public void copyFrom(TaskConfiguration taskConfiguration) {

        pausePeriodDate = toList(taskConfiguration.getPausePeriodDate());
        pausePeriodTime = toList(taskConfiguration.getPausePeriodTime());
        preferExecutorIdList = toList(taskConfiguration.getPreferExecutorIdList());


    }

    private List<String> toList(String str) {
        List<String> list = new ArrayList<>();
        if (str != null) {
            String[] split = str.split(",");
            for (String temp : split) {
                if (StringUtils.isNotBlank(temp)) {
                    list.add(temp.trim());
                }
            }
        }
        return list;
    }

}
